<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/29 17:19
 * Remark:
 */

namespace App\Http\Controllers\Verify;

use App\Consts\RedisConsts\RedisConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\EncryptionFunc;
use App\Func\RequestFunc;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class VerifyController extends Controller
{
    /**
     * 获取requestid新增
     * @return \Illuminate\Http\JsonResponse
     */
    public function addRequestId(): \Illuminate\Http\JsonResponse
    {
        $requestId = RequestFunc::getHeaderParams("Accept-RequestId");
        //验证当前验证器是否存在
        $isExist = Redis::exists($requestId);
        if(!$isExist){
            return ResponseFunc::returnJson(
                'error',
                null,
                "非法请求",
                ResponseStatusConsts::UNAUTHORIZED_401_RESPONSE_STATUS
            );
        }
        $requestId = md5(EncryptionFunc::encryptionHash(time()+rand(100,500)))."-".md5(EncryptionFunc::encryptionHash(time()+rand(500,1000)))."-". md5(EncryptionFunc::encryptionHash(time()+rand(500,1000)));
        Redis::setex($requestId,RedisConsts::REDIS_TOKEN_CONSTS,1);
        return ResponseFunc::returnJson(
            'ok',
            ['token' => $requestId],
            "VerifyCSr",
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }
}